﻿/* 
 * Capture Test NyARToolkitCSサンプルプログラム
 * --------------------------------------------------------------------------------
 * The MIT License
 * Copyright (c) 2008 nyatla
 * airmail(at)ebony.plala.or.jp
 * http://nyatla.jp/nyartoolkit/
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * 
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Text;
using System.Windows.Forms;

namespace SimpleLiteForm
{
    /// <summary>
    /// The application states.
    /// </summary>
    public enum ApplicationState
    {
        Start,
        LiveMode,
        Settings,
        Review
    }

    /// <summary>
    /// The main window.
    /// </summary>
    public partial class Form1 : Form
    {
        /// <summary>
        /// The frame rate.
        /// </summary>
        public const int FRAME_RATE = 30;

        /// <summary>
        /// The
        /// </summary>
        private LiveViewManager _sample;

        /// <summary>
        /// The presentation manager
        /// </summary>
        private PresentationManager _presentationManager;

        /// <summary>
        /// The keypoints in review page
        /// </summary>
        private List<KeyPoint> keyPoints;

        /// <summary>
        /// The current application state.
        /// </summary>
        private ApplicationState _applicationState;

        /// <summary>
        /// Creates a new Form1 instance.
        /// </summary>
        public Form1()
        {
            _presentationManager = new PresentationManager();

            InitializeComponent();
            keyPoints = new List<KeyPoint>();

            this.ClientSize = new Size(640,480);

            DXPanel.Visible = false;
            ReviewPanel.Visible = false;
            SettingsPanel.Visible = false;
            StartPanel.Visible = true;
            SelectApplicationState(ApplicationState.Start);

            _sample = new LiveViewManager(this, this.DXPanel);

            Timer renderingTimer = new Timer();
            renderingTimer.Interval = (1000 / FRAME_RATE);
            renderingTimer.Tick += new EventHandler(RenderUpdate);
            renderingTimer.Start();

            InitializeLanguages();
        }

        /// <summary>
        /// Initializes the combo box with the languages.
        /// </summary>
        private void InitializeLanguages()
        {
            language.Items.Add(new CultureInfo("en-US"));
            //language.Items.Add(new CultureInfo("zh-CN")); --> sry, but not supported in BING search :(
            language.Items.Add(new CultureInfo("de-DE"));
            language.Items.Add(new CultureInfo("es-ES"));
            language.Items.Add(new CultureInfo("fr-FR"));
            language.Items.Add(new CultureInfo("it-IT"));
            language.Items.Add(new CultureInfo("ja-JP"));
        }

        /// <summary>
        /// Selects a application state.
        /// </summary>
        /// <param name="state">The application state to select.</param>
        public void SelectApplicationState(ApplicationState state)
        {
            switch (state)
            {
                case ApplicationState.Start:
                    DXPanel.Visible = false;
                    StartPanel.Visible = true;
                    SettingsPanel.Visible = false;
                    ReviewPanel.Visible = false;
                    break;
                case ApplicationState.LiveMode:
                    DXPanel.Visible = true;
                    StartPanel.Visible = false;
                    SettingsPanel.Visible = false;
                    ReviewPanel.Visible = false;
                    break;
                case ApplicationState.Settings:
                    DXPanel.Visible = false;
                    StartPanel.Visible = false;
                    SettingsPanel.Visible = true;
                    ReviewPanel.Visible = false;
                    break;
                case ApplicationState.Review:
                    this.DXPanel.Visible = false;
                    this.StartPanel.Visible = false;
                    this.SettingsPanel.Visible = false;
                    this.ReviewPanel.Visible = true;
                    break;
            }

            // verify camera state
            if (state == ApplicationState.LiveMode)
            {
                Program.capture_device.StartCapture();
            }
            // if navigating from live view mode
            else if (_applicationState == ApplicationState.LiveMode)
            {
                Program.capture_device.StopCapture();
            }

            _applicationState = state;
        }

        public PresentationManager getPresentationManager()
        {
            return _presentationManager;
        }

        /// <summary>
        /// Renders the Direct3D nested window, if live mode is active.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The event args.</param>
        private void RenderUpdate(object sender, EventArgs e)
        {
            if (_applicationState == ApplicationState.LiveMode)
            {
                _sample.Update();

                // handle events
                Application.DoEvents();
            }    
        }

        /// <summary>
        /// Disposes the resources when the window gets closed.
        /// </summary>
        /// <param name="e">The sender.</param>
        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
            _sample.Dispose();
        }

        private void LiveViewButton_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(title.Text))
            {
                MessageBox.Show("Please specify a title.", "Attention");
            }
            else if (language.SelectedIndex == -1)
            {
                MessageBox.Show("Please select your prefered language.", "Attention");
            }
            else
            {
                _presentationManager.addPresentation(title.Text, DateTime.Now);

                SelectApplicationState(ApplicationState.LiveMode);
            }
            
        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void StartPanel_Paint(object sender, PaintEventArgs e)
        {
            //set default value here

            displayRecord();
            
        }

        private void displayRecord()
        {
            //set record gridview
            DataTable dataTable = new DataTable();
            dataTable = _presentationManager.displayRecord();

            //data grid
            record.DataSource = dataTable;
            record.Columns[0].Width = 200;
            record.Columns[1].Width = 200;

        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            
        }

        private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void record_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

            loadReviewPage(e.RowIndex);
            SelectApplicationState(ApplicationState.Review);
        }

        void loadReviewPage(int selectIndex)
        {
            //load selected presentation review
            
            Presentation pres = _presentationManager.getPresentation(selectIndex);

            string[] data = pres.getRecord();
            reviewTitle.Text = data[0];
            reviewDate.Text = data[1];
            reviewBegin.Text = pres.getBeginTime().ToLongTimeString();
            reviewEnd.Text = pres.getEndTime().ToLongTimeString();

            //display key points
            for (int index = 0; index < keyPoints.Count; index++)
            {
                keyPoints[index].dispose();
            }
            keyPoints.Clear();
            for (int index = 0; index < pres.getScreenshotsNum(); index++)
            {
                KeyPoint kp = new KeyPoint(pres, index, timeBar, pictureDisplay);
                keyPoints.Add(kp);
            }

            
        }

        private void title_TextChanged(object sender, EventArgs e)
        {

        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }

        private void ReviewPanel_Paint(object sender, PaintEventArgs e)
        {

        }

        private void ReviewPanel_Paint_1(object sender, PaintEventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            SelectApplicationState(ApplicationState.Start);
        }

        private void reviewBackBtn_Click(object sender, EventArgs e)
        {
            SelectApplicationState(ApplicationState.Start);
        }

        private void testBtn_Click(object sender, EventArgs e)
        {

        }

        private void language_SelectedIndexChanged(object sender, EventArgs e)
        {
            var cbx = sender as ComboBox;

            if (cbx != null)
            {
                var language = cbx.SelectedItem as CultureInfo;

                if (language != null)
                    Settings.PreferedLanguage = language;
            }
        }
    }
}
